<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Cmspage
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Cmspage_IndexController extends Core_Controller_Action_Standard {
	protected $_navigation;
	
	public function init() {
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			if (0 !== ($cmspage_id = ( int ) $this->_getParam ( 'cmspage_id' )) && null !== ($cmspage = Engine_Api::_ ()->getItem ( 'cmspage', $cmspage_id ))) {
				Engine_Api::_ ()->core ()->setSubject ( $cmspage );
			}
		}
		
		$this->_helper->requireUser->addActionRequires ( array ('create', 'delete', 'edit', 'manage', 'success', 'coordinate' ) );
		
		$this->_helper->requireSubject->setActionRequireTypes ( array ('delete' => 'cmspage', 'edit' => 'cmspage', 'success' => 'cmspage', 'publish' => 'cmspage', 'view' => 'cmspage' ) );
	}
	public function indexAction() {
		$this->publicViewPermissionRequires ();
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$this->view->form = $form = new Cmspage_Form_Search ();
		
		if (! $viewer->getIdentity ()) {
			$form->removeElement ( 'show' );
		}
		
		$form->setAction ( $this->getFrontController ()->getRouter ()->assemble ( array (), 'cmspage_browse' ) );
		
		$this->view->navigation = $this->getNavigation ();
		$this->view->can_create = $this->_helper->requireAuth ()->setAuthParams ( 'cmspage', null, 'create' )->checkRequire ();
		
		// Recent Cmspages
		$recentPostedValues = array ('limit' => ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.homerecentmax', 5 ), 'order' => 'creation_date', 'order_direction' => 'DESC' );
		$this->view->recentPostedPaginator = Engine_Api::_ ()->cmspage ()->getPublishedCmspagesPaginator ( $recentPostedValues );
		
		// Sticky
		$this->view->stickyPaginator = Engine_Api::_ ()->cmspage ()->getPublishedCmspagesPaginator ( array ('limit' => ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.homestickymax', 3 ), 'order' => 'random', 'sticky' => 1 ) );
		
		/*
    // Featured
    $this->view->featuredPaginator = Engine_Api::_()->cmspage()->getPublishedCmspagesPaginator(array(
      'limit' => (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('cmspage.homefeaturedmax', 3),
      'order' => 'random',
      'featured' => 1,
    ));
    */
		
		// Popular Tags
		if (($limit = ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.hometagsmax', 50 )) > 0) {
			$this->view->tags = Engine_Api::_ ()->cmspage ()->getPopularTags ( array ('limit' => $limit ) );
		}
		
		$this->view->categories = Engine_Api::_ ()->cmspage ()->getCategories ();
	
	}
	
	// NONE USER SPECIFIC METHODS
	public function browseAction() {
		$this->publicViewPermissionRequires ();
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$this->view->navigation = $this->getNavigation ();
		$this->view->can_create = $this->_helper->requireAuth ()->setAuthParams ( 'cmspage', null, 'create' )->checkRequire ();
		
		$this->view->form = $form = new Cmspage_Form_Search ();
		
		if (! $viewer->getIdentity ()) {
			$form->removeElement ( 'show' );
		}
		
		$values = array ();
		// Populate form data
		if ($form->isValid ( $this->_getAllParams () )) {
			$values = $form->getValues ();
		}
		
		$values = Engine_Api::_ ()->getApi ( 'filter', 'radcodes' )->removeKeyEmptyValues ( $values );
		
		if (! empty ( $values ['category'] )) {
			$this->view->categoryObject = Engine_Api::_ ()->cmspage ()->getCategory ( $values ['category'] );
		}
		
		$this->view->formValues = $values;
		
		if ($viewer->getIdentity () && @$values ['show'] == 1) {
			$values ['users'] = array ();
			foreach ( $viewer->membership ()->getMembersInfo ( true ) as $memberinfo ) {
				$values ['users'] [] = $memberinfo->user_id;
			}
		}
		
		// check to see if request is for specific user's cmspages
		$user_id = $this->_getParam ( 'user' );
		if ($user_id)
			$values ['user_id'] = $user_id;
		
		$this->view->assign ( $values );
		
		// items needed to show what is being filtered in browse page
		if (! empty ( $values ['tag'] )) {
			$this->view->tagObject = Engine_Api::_ ()->getItem ( 'core_tag', $values ['tag'] );
		}
		
		$values ['limit'] = ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.page', 20 );
		$values ['pre_order'] = ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.sorting', 0 );
		$values ['distance_unit'] = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.distanceunit', Radcodes_Lib_Helper_Unit::UNIT_MILE );
		
		//Engine_Api::_()->getApi('debug','radcodes')->log($values,'values');
		

		$this->view->paginator = $paginator = Engine_Api::_ ()->cmspage ()->getPublishedCmspagesPaginator ( $values );
		$paginator->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
		
		$this->view->google_map = $google_map = Engine_Api::_ ()->getApi ( 'map', 'radcodes' )->getInstance ( 'cmspage_browse' );
		
		$this->view->show_page_details = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.pagedetails', 1 );
	
	}
	
	public function manageAction() {
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$this->view->can_create = $this->_helper->requireAuth ()->setAuthParams ( 'cmspage', null, 'create' )->checkRequire ();
		$this->view->can_edit = $this->_helper->requireAuth ()->setAuthParams ( 'cmspage', null, 'edit' )->checkRequire ();
		$this->view->can_delete = $this->_helper->requireAuth ()->setAuthParams ( 'cmspage', null, 'delete' )->checkRequire ();
		$this->view->allowed_upload = Engine_Api::_ ()->authorization ()->getPermission ( $viewer->level_id, 'cmspage', 'photo' );
		
		$this->view->navigation = $this->getNavigation ();
		$this->view->form = $form = new Cmspage_Form_Search ();
		$form->setAction ( Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( array (), 'cmspage_manage', true ) );
		$form->removeElement ( 'show' );
		
		$values = array ();
		// Populate form data
		if ($form->isValid ( $this->_getAllParams () )) {
			$values = $form->getValues ();
		}
		
		$values = Engine_Api::_ ()->getApi ( 'filter', 'radcodes' )->removeKeyEmptyValues ( $values );
		$this->view->formValues = $values;
		
		$values ['user_id'] = $viewer->getIdentity ();
		$this->view->assign ( $values );
		
		$values ['limit'] = ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.page', 20 );
		
		// Get paginator
		$this->view->paginator = $paginator = Engine_Api::_ ()->cmspage ()->getCmspagesPaginator ( $values );
		$paginator->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
		
		// maximum allowed cmspages
		$this->view->quota = $quota = Engine_Api::_ ()->authorization ()->getPermission ( $viewer->level_id, 'cmspage', 'max' );
		$this->view->current_count = $paginator->getTotalItemCount ();
	
	}
	
	public function viewAction() {
		$this->view->viewer = $viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->cmspage = $cmspage = Engine_Api::_ ()->core ()->getSubject ( 'cmspage' );
		
		if (! Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.public', 1 ) && ! $this->_helper->requireUser ()->isValid ()) {
			return;
		}
		
		// logged in && no view permission => show no permission
		if ($this->_helper->requireUser ()->checkRequire () && ! $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'view' )->isValid ()) {
			return;
		} else if (! $this->_helper->requireUser ()->checkRequire ()) {
			if (! $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'view' )->checkRequire ()) {
				return $this->_forward ( 'requireuser', 'error', 'core' );
			}
		}
		
		$this->view->owner = $owner = Engine_Api::_ ()->user ()->getUser ( $cmspage->owner_id );
		
		$this->view->canEdit = $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'edit' )->checkRequire ();
		$this->view->canUpload = $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'photo' )->checkRequire ();
		$this->view->canDelete = $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'delete' )->checkRequire ();
		
		if ($viewer->getIdentity () != $owner->getIdentity ()) {
			$cmspage->view_count ++;
			$cmspage->save ();
		}
		
		$this->view->cmspage = $cmspage;
		if ($cmspage->photo_id) {
			$this->view->main_photo = $cmspage->getPhoto ( $cmspage->photo_id );
		}
		// get tags
		$this->view->cmspageTags = $cmspage->tags ()->getTagMaps ();
		$this->view->userTags = $cmspage->tags ()->getTagsByTagger ( $cmspage->getOwner () );
		
		$view = $this->view;
		$view->addHelperPath ( APPLICATION_PATH . '/application/modules/Fields/View/Helper', 'Fields_View_Helper' );
		$this->view->fieldStructure = $fieldStructure = Engine_Api::_ ()->fields ()->getFieldsStructurePartial ( $cmspage );
		
		// album material
		$this->view->album = $album = $cmspage->getSingletonAlbum ();
		$this->view->paginator = $paginator = $album->getCollectiblesPaginator ();
		$paginator->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
		$paginator->setItemCountPerPage ( Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.gallery', 5 ) );
		
		if ($cmspage->category_id != 0)
			$this->view->category = Engine_Api::_ ()->cmspage ()->getCategory ( $cmspage->category_id );
		$this->view->userCategories = Engine_Api::_ ()->cmspage ()->getUserCategories ( $this->view->cmspage->owner_id );
		
		$other_cmspages_params = array ('exclude_cmspage_ids' => array ($cmspage->getIdentity () ), 'user_id' => $cmspage->owner_id, 'limit' => 5 );
		$this->view->paginatorCmspages = $paginator = Engine_Api::_ ()->cmspage ()->getPublishedCmspagesPaginator ( $other_cmspages_params );
		
		$display_fields = array ('showlayout' => 1, 'showtitle' => 0, 'showphoto' => 0, 'showinfo' => 0, 'showtool' => 0, 'showcomment' => 0 );
		
		foreach ( $display_fields as $field_key => $field_default ) {
			$setting_field_value = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.' . $field_key, $field_default );
			$this->view->$field_key = ($setting_field_value == 1) || ($setting_field_value == 2 && $cmspage->$field_key);
		}
		// $this->view->headStyle()->appendStyle($cmspage->header_style);
		$this->view->allowed_script = Engine_Api::_ ()->authorization ()->getPermission ( $owner->level_id, 'cmspage', 'script' ) && $cmspage->header_script;
		$this->view->allowed_style = Engine_Api::_ ()->authorization ()->getPermission ( $owner->level_id, 'cmspage', 'style' ) && $cmspage->header_style;
		
		if (! $this->view->showlayout) {
			//$this->_helper->layout->disableLayout();
			$this->_helper->layout->setLayout ( 'default-simple' );
		}
	}
	
	public function createAction() {
		//if( !$this->_helper->requireUser()->isValid() ) return;
		if (! $this->_helper->requireAuth ()->setAuthParams ( 'cmspage', null, 'create' )->isValid ())
			return;
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$this->view->navigation = $this->getNavigation ();
		$this->view->form = $form = new Cmspage_Form_Create ();
		// set up data needed to check quota
		$values ['user_id'] = $viewer->getIdentity ();
		$paginator = $this->_helper->api ()->getApi ( 'core', 'cmspage' )->getCmspagesPaginator ( $values );
		
		$this->view->addHelperPath ( APPLICATION_PATH . '/application/modules/Fields/View/Helper', 'Fields_View_Helper' );
		
		$this->view->quota = $quota = Engine_Api::_ ()->authorization ()->getPermission ( $viewer->level_id, 'cmspage', 'max' );
		$this->view->current_count = $paginator->getTotalItemCount ();
		
		// If not post or form not valid, return
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$table = Engine_Api::_ ()->getItemTable ( 'cmspage' );
			$db = $table->getAdapter ();
			$db->beginTransaction ();
			
			try {
				$featured = Engine_Api::_ ()->authorization ()->getPermission ( $viewer->level_id, 'cmspage', 'featured' ) ? 1 : 0;
				$sticky = Engine_Api::_ ()->authorization ()->getPermission ( $viewer->level_id, 'cmspage', 'sticky' ) ? 1 : 0;
				
				// Create cmspage
				$values = array_merge ( $form->getValues (), array ('owner_type' => $viewer->getType (), 'owner_id' => $viewer->getIdentity (), 'featured' => $featured, 'sticky' => $sticky ) );
				
				if ($values ['home']) {
					Engine_Api::_ ()->cmspage ()->clearCmspageHome ( $viewer->getIdentity () );
				}
				
				$cmspage = $table->createRow ();
				$cmspage->setFromArray ( $values );
				$cmspage->save ();
				
				// Set photo
				if (! empty ( $values ['photo'] )) {
					$cmspage->setPhoto ( $form->photo );
				}
				
				// Add tags
				$tags = preg_split ( '/[,]+/', $values ['tags'] );
				$tags = array_filter ( array_map ( "trim", $tags ) );
				$cmspage->tags ()->addTagMaps ( $viewer, $tags );
				
				$customfieldform = $form->getSubForm ( 'customField' );
				$customfieldform->setItem ( $cmspage );
				$customfieldform->saveValues ();
				
				// CREATE AUTH STUFF HERE
				$auth = Engine_Api::_ ()->authorization ()->context;
				$roles = array ('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone' );
				
				$auth_keys = array ('view' => 'everyone', 'comment' => 'registered' );
				
				foreach ( $auth_keys as $auth_key => $auth_default ) {
					$auth_value = isset ( $values ['auth_' . $auth_key] ) ? $values ['auth_' . $auth_key] : $auth_default;
					$authMax = array_search ( $auth_value, $roles );
					
					foreach ( $roles as $i => $role ) {
						$auth->setAllowed ( $cmspage, $role, $auth_key, ($i <= $authMax) );
					}
				}
				
				// Add activity only if cmspage is published
				if ($cmspage->isPublished ()) {
					$action = Engine_Api::_ ()->getDbtable ( 'actions', 'activity' )->addActivity ( $viewer, $cmspage, 'cmspage_new' );
					if ($action != null) {
						Engine_Api::_ ()->getDbtable ( 'actions', 'activity' )->attachActivity ( $action, $cmspage );
					}
				}
				
				// Commit
				$db->commit ();
				
				// Redirect
				$allowed_upload = Engine_Api::_ ()->authorization ()->getPermission ( $viewer->level_id, 'cmspage', 'photo' );
				
				if ($allowed_upload) {
					return $this->_helper->redirector->gotoRoute ( array ('cmspage_id' => $cmspage->cmspage_id ), 'cmspage_success', true );
				} else {
					return $this->_helper->redirector->gotoRoute ( array (), 'cmspage_manage', true );
				}
			
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		}
	}
	
	public function editAction() {
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->cmspage = $cmspage = Engine_Api::_ ()->core ()->getSubject ( 'cmspage' );
		
		if (! $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'edit' )->isValid ()) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		
		// Get navigation
		$navigation = $this->getNavigation ();
		$this->view->navigation = $navigation;
		
		$this->view->form = $form = new Cmspage_Form_Edit ( array ('item' => $cmspage ) );
		
		// only for create
		$form->removeElement ( 'photo' );
		
		$form->populate ( $cmspage->toArray () );
		
		$auth = Engine_Api::_ ()->authorization ()->context;
		$roles = array ('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone' );
		$auth_keys = array ('view' => 'everyone', 'comment' => 'registered' );
		
		$this->view->addHelperPath ( APPLICATION_PATH . '/application/modules/Fields/View/Helper', 'Fields_View_Helper' );
		
		// Save cmspage entry
		if (! $this->getRequest ()->isPost ()) {
			
			// prepare tags
			$cmspageTags = $cmspage->tags ()->getTagMaps ();
			
			$tagString = '';
			foreach ( $cmspageTags as $tacmspage ) {
				if ($tagString !== '')
					$tagString .= ', ';
				$tagString .= $tacmspage->getTag ()->getTitle ();
			}
			
			$this->view->tagNamePrepared = $tagString;
			$form->tags->setValue ( $tagString );
			
			foreach ( $auth_keys as $auth_key => $auth_default ) {
				$auth_field = 'auth_' . $auth_key;
				
				foreach ( $roles as $i => $role ) {
					if (isset ( $form->$auth_field->options [$role] ) && 1 === $auth->isAllowed ( $cmspage, $role, $auth_key )) {
						$form->$auth_field->setValue ( $role );
					}
				}
			}
			
			return;
		}
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		// Process
		

		// handle save for tags
		$values = $form->getValues ();
		$tags = preg_split ( '/[,]+/', $values ['tags'] );
		$tags = array_filter ( array_map ( "trim", $tags ) );
		
		$db = Engine_Db_Table::getDefaultAdapter ();
		$db->beginTransaction ();
		try {
			
			$cmspage->setFromArray ( $values );
			$cmspage->modified_date = date ( 'Y-m-d H:i:s' );
			
			$cmspage->tags ()->setTagMaps ( $viewer, $tags );
			$cmspage->save ();
			
			// Save custom fields
			$customfieldform = $form->getSubForm ( 'customField' );
			$customfieldform->setItem ( $cmspage );
			$customfieldform->saveValues ();
			
			// CREATE AUTH STUFF HERE
			$values = $form->getValues ();
			foreach ( $auth_keys as $auth_key => $auth_default ) {
				$auth_value = isset ( $values ['auth_' . $auth_key] ) ? $values ['auth_' . $auth_key] : $auth_default;
				$authMax = array_search ( $auth_value, $roles );
				
				foreach ( $roles as $i => $role ) {
					$auth->setAllowed ( $cmspage, $role, $auth_key, ($i <= $authMax) );
				}
			}
			
			// Add activity only if cmspage is published
			$action = Engine_Api::_ ()->getDbtable ( 'actions', 'activity' )->getActionsByObject ( $cmspage );
			if (count ( $action->toArray () ) <= 0 && $cmspage->isPublished ()) {
				
				if ($viewer->getIdentity () != $cmspage->owner_id) {
					$owner = Engine_Api::_ ()->user ()->getUser ( $cmspage->owner_id );
				} else {
					$owner = $viewer;
				}
				
				$action = Engine_Api::_ ()->getDbtable ( 'actions', 'activity' )->addActivity ( $owner, $cmspage, 'cmspage_new' );
				if ($action != null) {
					Engine_Api::_ ()->getDbtable ( 'actions', 'activity' )->attachActivity ( $action, $cmspage );
				}
			}
			
			// Rebuild privacy
			$actionTable = Engine_Api::_ ()->getDbtable ( 'actions', 'activity' );
			foreach ( $actionTable->getActionsByObject ( $cmspage ) as $action ) {
				$actionTable->resetActivityBindings ( $action );
			}
			$db->commit ();
			
			$savedChangesNotice = Zend_Registry::get ( 'Zend_Translate' )->_ ( "Your changes were saved." );
			$form->addNotice ( $savedChangesNotice );
			$customfieldform->removeElement ( 'submit' );
		
		//$this->_helper->getHelper('FlashMessenger')->addMessage($savedChangesNotice);
		

		//return $this->_helper->redirector->gotoRoute(array(), 'cmspage_manage', true);
		//return $this->_helper->redirector->gotoRoute(array('cmspage_id'=>$cmspage->cmspage_id), 'cmspage_edit', true);
		} catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
	}
	
	public function deleteAction() {
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->cmspage = $cmspage = Engine_Api::_ ()->core ()->getSubject ( 'cmspage' );
		
		//if( $viewer->getIdentity() != $cmspage->owner_id && !$this->_helper->requireAuth()->setAuthParams($cmspage, null, 'edit')->isValid())
		if (! $this->_helper->requireAuth ()->setAuthParams ( $cmspage, null, 'delete' )->isValid ())
			return;
		
		// Get navigation
		$navigation = $this->getNavigation ();
		$this->view->navigation = $navigation;
		
		if ($this->getRequest ()->isPost () && $this->getRequest ()->getPost ( 'confirm' ) == true) {
			$this->view->cmspage->delete ();
			return $this->_helper->redirector->gotoRoute ( array (), 'cmspage_manage', true );
		}
	}
	
	public function successAction() {
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->cmspage = $cmspage = Engine_Api::_ ()->core ()->getSubject ( 'cmspage' );
		
		if ($viewer->getIdentity () != $cmspage->owner_id) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		
		$this->view->navigation = $this->getNavigation ();
		
		if ($this->getRequest ()->isPost () && $this->getRequest ()->getPost ( 'confirm' ) == true) {
			return $this->_helper->redirector->gotoRoute ( array ('controller' => 'photo', 'action' => 'upload', 'subject' => $cmspage->getGuid () ), 'cmspage_extended', true );
		}
	}
	
	public function getNavigation($active = false) {
		if (is_null ( $this->_navigation )) {
			$navigation = $this->_navigation = new Zend_Navigation ();
			
			if (Engine_Api::_ ()->user ()->getViewer ()->getIdentity ()) {
				$navigation->addPage ( array ('label' => Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Browse Pages' ), 'route' => 'cmspage_browse', 'module' => 'cmspage', 'controller' => 'index', 'action' => 'browse' ) );
				
				$navigation->addPage ( array ('label' => Zend_Registry::get ( 'Zend_Translate' )->_ ( 'My Pages2' ), 'route' => 'cmspage_manage', 'module' => 'cmspage', 'controller' => 'index', 'action' => 'manage', 'active' => $active ) );
				if ($this->_helper->requireAuth ()->setAuthParams ( 'cmspage', null, 'create' )->checkRequire ()) {
					$navigation->addPage ( array ('label' => Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Post New Page' ), 'route' => 'cmspage_create', 'module' => 'cmspage', 'controller' => 'index', 'action' => 'create' ) );
				}
			}
		}
		return $this->_navigation;
	}
	
	public function handleArchiveList($results) {
		$archive_list = array ();
		
		foreach ( $results as $result ) {
			$cmspage_date = strtotime ( $result ['period'] );
			
			$date_info = Radcodes_Lib_Helper_Date::archive ( $cmspage_date );
			$date_start = $date_info ['date_start'];
			
			if (! isset ( $archive_list [$date_start] )) {
				$archive_list [$date_start] = $date_info;
				$archive_list [$date_start] ['count'] = $result ['total'];
			} else {
				$archive_list [$date_start] ['count'] += $result ['total'];
			}
		}
		
		return $archive_list;
	}
	
	protected function publicViewPermissionRequires() {
		// NOTE: boolean return is not really needed - since it auto redirect to login/no permission screen
		

		// require log in --- and -- not logged in => show log in screen
		if (! Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.public', 1 ) && ! $this->_helper->requireUser ()->isValid ()) {
			return false;
		}
		
		// logged in && no view permission => show no permission
		if ($this->_helper->requireUser ()->checkRequire () && ! $this->_helper->requireAuth ()->setAuthParams ( 'cmspage', null, 'view' )->isValid ()) {
			return false;
		}
		
		return true;
	}

}

